/**
 * Created by YiFeng on 2017/4/29.
 */
!(function(){
    var __functionForUdesktop=function () {
        Ladder.NS("Udesktop");
        Ladder.Udesktop=function(option){
            this.container=null;
            this.index=0;
            this.setting={
                site:"开发者平台",
                url:"",
                username:""
            }
            $.extend(this.setting,option);
            this.init();
        }
        Ladder.Udesktop.prototype={
            init:function(){
                var me=this;
                me.container = $('<div class="ladder-udesktop" style="height: 100%;width:100%;padding: 24px 0 0 0"></div>').prependTo(document.body)
                var $WindowTop = $('<div class="WindowTop">').appendTo(me.container);
                //系统头信息
                $('<div class="SystemHead">').appendTo($WindowTop)
                    //站点名称
                    .append('<div class="site fl"><i class="iconfont sign">&#xe741;</i><span class="sitename">'+me.setting.site+'</span></div>')
                    //当前用户名
                    .append('<div class="fr userinfo"><span class="username">'+me.setting.username+'</span><i class="iconfont">&#xe770;</i></div>')
                    //用户设置菜单
                    .append('<div class="usersetting"><ul></ul></div>');
                //当前页面信息与操作按钮
                $('<div class="TabPageHead">').appendTo($WindowTop)
                    .append('<div class="pageinfo"><i class="iconfont sign">&#xe741;</i><span class="title"></span></div>')
                    //关闭按钮
                    .append('<div class="close controlbtn"><i class="iconfont">&#xe795;</i></div>')
                    //最小化
                    .append('<div class="controlbtn"><span class="minimize" style="margin: 0 4px;"></span></div>')
                    //刷新
                    .append('<div class="refresh controlbtn"><i class="iconfont">&#xe7e4;</i></div>');
                //大区视图
                var AreaView=$("<div class='AreaView'></div>").appendTo(me.container);
                //任务栏
                AreaView.append('<div class="Taskbar"><ul><li class="home"><img src="../images/Ubuntu1-64.png" width="48" height="48"></li></ul></div>')
                //主视图
                var BodyView=$('<div class="BodyView"></div>').appendTo(AreaView);
                //桌面图标
                BodyView.append('<div class="desktop"><ul class="desktopmenus"></ul></div>');
                //页面view
                BodyView.append('<div class="pages"></div>');
                //LaunchView
                BodyView.append('<div class="launchView"><div class="launchHeadBtns">' +
                    '<span class="iconfont launchclosebtn">&#xe7e7;</span>' +
                    '<span class="iconfont launchclosebtn">&#xe755;</span>' +
                    '<span class="iconfont launchclosebtn">&#xe733;</span>' +
                    '</div><ul></ul></div>');

                //绑定操作按钮事件
                $WindowTop.find(".TabPageHead .close").click(function () {
                    var pageno = $(this).data("pageno");
                    me.container.find(".pages #" + pageno).remove();
                    me.container.find(".Taskbar li[data-pageno='" + pageno + "']").remove();
                    me.container.find(".pages").hide();
                    me.container.find(".desktop").show();
                    me.container.find(".TabPageHead").hide();
                    me.container.find(".SystemHead").show();

                });
                $WindowTop.find(".TabPageHead .minimize").click(function() {
                    me.container.find(".pages").hide();
                    me.container.find(".desktop").show();
                    me.container.find(".TabPageHead").hide();
                    me.container.find(".SystemHead").show();
                });
                $WindowTop.find(".TabPageHead .refresh").click(function() {
                    var pageno = me.container.find(".TabPageHead .close").data("pageno");
                    var iframe = me.container.find(".pages #" + pageno)[0];
                    if(iframe){
                        iframe.contentWindow.location.reload(true);//绕过浏览器缓存
                    }
                });
                $WindowTop.find(".userinfo").mouseenter(function() {
                    $WindowTop.find(".usersetting").css("right", "0px");
                });
                $WindowTop.find(".SystemHead").mouseleave(function() {
                    $WindowTop.find(".usersetting").css("right", "-100px");
                });
                me.container.find(".Taskbar>ul>.home,.launchView .launchHeadBtns .launchclosebtn").click(function() {
                    me.container.find(".launchView").toggle(500,function(){
                        if(!me.container.find(".launchView").is(":hidden")){
                            me.container.find(".TabPageHead").hide();
                            me.container.find(".SystemHead").show();
                        }else{
                            me.container.find(".SystemHead").hide();
                            me.container.find(".TabPageHead").show();
                        }
                    });
                });
            },
            open:function(settings, create){
                var me=this;
                var $iframe = undefined;
                if(create) {
                    //命令菜单
                    if(settings.type==1){
                        var sider=new Ladder.Sider({
                            title:settings.title
                        });
                        var html = Ladder.tpl("#commandview",settings);
                        var $view = $(html);
                        sider.content($view);
                        $view.find(".commandbtn").click(function(){
                            $(this).prop("disabled",true);
                            $view.next(".commandresult").val("正在请求中...");
                            var url = Ladder.Service.parseUrl(settings.url);
                            if(!url)return;
                            webajax(url,function(result){
                                $(this).prop("disabled",false);
                                $view.next(".commandresult").val(JSON.stringify(result));
                            })
                        });
                        sider.open();
                        return;
                    }
                    //页面菜单
                    else{
                        $iframe = me.container.find(".pages iframe[data-id='" + settings.id + "']");
                        if($iframe.length < 1 || !settings.isonly) {
                            var pageno = "pageno" + me.index++;
                            var iframe = $("<iframe class='iframepage' id='" + pageno + "'></iframe>");
                            var $NavMenu = me.container.find(".Taskbar>ul");
                            //iframe.attr("src",Ladder.Service.parseUrl(settings.url.replace("#username#", loginuser))).attr("data-id", settings.id);
                            iframe.attr("src",Ladder.Service.parseUrl(settings.url)).attr("data-id", settings.id);
                            me.container.find(".pages").append(iframe);
                            me.container.find(".TabPageHead .title").html(settings.title);
                            me.container.find(".TabPageHead .close").data("id", settings.id).data("pageno", pageno);
                            iframe.siblings().hide();
                            me.container.find(".desktop").hide();
                            me.container.find(".pages").show();
                            me.container.find(".TabPageHead").show();
                            me.container.find(".SystemHead").hide();
                            var $li = $("<li data-pageno='" + pageno + "' data-id='" + settings.id + "' title='" + settings.title + "'></li>").appendTo($NavMenu);
                            $li.append('<img src="' + settings.icon + '" width="48" height="48">');
                            $li.click(function() {
                                $li.addClass("current").siblings().removeClass("current");
                                me.open({ pageno: pageno, id: settings.id, title: settings.title }, false);
                            });
                            $li.addClass("current").siblings().removeClass("current");
                            return;
                        }
                    }
                } else {
                    $iframe = $(".pages #" + settings.pageno);
                }
                if(!$iframe.length) return;
                $iframe.show().siblings().hide();
                me.container.find(".TabPageHead .title").html(settings.title);
                me.container.find(".TabPageHead .close").data("id", settings.id).data("pageno", $iframe.attr("id"));
                me.container.find(".TabPageHead").show();
                me.container.find(".desktop").hide();
                me.container.find(".pages").show();
                me.container.find(".SystemHead").hide();
                if(!me.container.find(".launchView").is(':hidden')){
                    me.container.find(".launchView").toggle();
                }
            },
            initMenus:function(data){
                if(!data) return;
                var me=this;
                var $launch = me.container.find(".launchView>ul");
                var $desktopmenus = me.container.find(".desktopmenus");
                var height=$desktopmenus.height();
                var size=Math.floor(height/90);
                var margintop = (height-12-(size*90))/2-10;
                if(margintop<0)margintop=0;
                if(margintop>10)margintop=10;
                var left=0;
                var index=0;
                data.forEach(function(item) {
                    var $item = $("<li></li>");
                    if(item.children && item.children.length > 0) {
                        $launch.append($item);
                        $("<div>" + item.title + "</div>").appendTo($item);
                        var $ul = $("<ul></ul>").appendTo($item);
                        item.children.forEach(function(it) {
                            var $li = $("<li>" + it.title + "</li>");
                            $li.click(function() {
                                me.open(it, true);
                                me.container.find(".launchView").toggle(500);
                            });
                            $ul.append($li);
                            if(it.isquick) {
                                var $deskitem = $("<li class='desktopitem' title='"+it.title+"'></li>").appendTo($desktopmenus);
                                $deskitem.data("position",{left:left,top:margintop+(83*index++)});
                                $deskitem.animate($deskitem.data("position"));
                                if(index>size){
                                    left+=100;
                                    index=0;
                                }
                                $deskitem.append('<span class="icon"><img src="' + it.icon + '" width="32px" height="32px"></span>');
                                $deskitem.append('<div class="menutitle"><i></i><span>' + it.title + '</span><s></s></div>');
                                $deskitem.click(function() { me.open(it, true) });
                            }
                        });
                    }
                });
            },
            addSettingMenu:function(title,callback){
                var me=this;
                var $usersettings = me.container.find(".usersetting");
                $('<li><a>'+title+'</a></li>').appendTo($usersettings).click(function () {
                    callback();
                });
            },
            requestData:function(url,data,method){
                var me=this;
                webajax(this.setting.url, function(ret) {
                    if(ret.statusCode == 200) me.initMenus(ret.data);
                });
            }
        }
        Ladder.NS("Sider");
        Ladder.Sider=function(option){
            this.own=$('<div class="sideview"></div>').appendTo($("#siders"));
            this.settings={
                width:300,
                height:300,
                title:"操作处理",
                dispose:true
            };
            $.extend(this.settings, option);
            this.init();
        };
        Ladder.Sider.prototype = {
            init: function () {
                var me = this;
                var $head = $(' <div class="sidehead"><div class="caption"></div><div class="closeview"><i class="iconfont closebtn">&#xe608;</i></div><div>').appendTo(me.own);
                me.own.css({
                    width: me.settings.width,
                    height: me.settings.height
                });
                $head.find(".caption").html(me.settings.title);
                $head.find(".closebtn").click(function () {
                    me.close();
                });
                $("<div class='sidecontent'></div>").appendTo(me.own);
            },
            content:function(html){
                if(html){
                    this.own.find(".sidecontent").html(html);
                }else {
                    return this.own.find(".sidecontent");
                }
            },
            open:function(){
                var me=this;
                me.own.animate({"margin-left":-1*me.settings.width},800);
            },
            destroy: function () {
                var me = this;
                me.own.remove();
            },
            close:function(){
                this.own.css({
                    "margin-left":0
                });
                if(this.settings.dispose){
                    this.own.remove();
                    delete this;
                }
            }
        }
    };
    if(typeof define === "function" && define.amd){
        define(['ladder-form'],__functionForUdesktop);
    }else{
        __functionForUdesktop();
    }
})();


